﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;

namespace Alvin.Common.Utils
{
    public class FontUtil
    {
        /// <summary>
        /// 将磅值转化成像素值
        /// </summary>
        /// <param name="emSize"></param>
        /// <returns></returns>
        public static float EmSizeToPix(float emSize)
        {

            return GetDpi()* emSize / 72;
        }

        /// <summary>
        /// 获取系统DPI值
        /// </summary>
        /// <returns></returns>
        public static int GetDpi()
        {
            var dpi = 96;
            using (var mc = new ManagementClass("Win32_DesktopMonitor"))
            {
                using (var moc = mc.GetInstances())
                {
                    //var PixelsPerXLogicalInch = 0;  // dpi for x
                    //var PixelsPerYLogicalInch = 0;  // dpi for y
                    foreach (ManagementObject each in moc)
                    {
                        //PixelsPerXLogicalInch = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString()));
                        //PixelsPerYLogicalInch = int.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString()));
                        dpi = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString()));
                        break;
                    }
                    //Console.WriteLine("PixelsPerXLogicalInch:" + PixelsPerXLogicalInch.ToString());
                    //Console.WriteLine("PixelsPerYLogicalInch:" + PixelsPerYLogicalInch.ToString());
                    //Console.Read();
                }
            }
            return dpi;
        }
    }
}
